package io.nutz.nutzsite.module.exam.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;

import io.nutz.nutzsite.module.exam.models.Paperdef;
import io.nutz.nutzsite.module.exam.models.Qdb;
import io.nutz.nutzsite.module.exam.models.Qtype;
import io.nutz.nutzsite.module.exam.services.PaperdefService;
import io.nutz.nutzsite.module.exam.services.QdbService;
import io.nutz.nutzsite.common.base.Result;

import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Lang;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.plugins.slog.annotation.Slog;

import io.nutz.nutzsite.common.utils.ShiroUtils;
import io.nutz.nutzsite.common.utils.StringUtils;

import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Date;
/**
 * 试卷定义 信息操作处理
 * 
 * @author lrw
 * @date 2020-04-11
 */
@IocBean
@At("/exam/paperdef")
public class PaperdefController {
	private static final Log log = Logs.get();

	@Inject
	private PaperdefService paperdefService;
	@Inject
	private QdbService qdbService;
	
	@RequiresPermissions("exam:paperdef:view")
	@At("")
	@Ok("th:/exam/paperdef/paperdef.html")
	public void index(HttpServletRequest req) {

	}

	/**
	 * 查询试卷定义列表
	 */
	@RequiresPermissions("exam:paperdef:list")
	@At
	@Ok("json")
	public Object list(@Param("pageNum")int pageNum,
					   @Param("pageSize")int pageSize,
					   @Param("name") String name,
					   @Param("beginTime") Date beginTime,
					   @Param("endTime") Date endTime,
					   @Param("orderByColumn") String orderByColumn,
					   @Param("isAsc") String isAsc,
					   HttpServletRequest req) {
		Cnd cnd = Cnd.NEW();
		if (!Strings.isBlank(name)){
			cnd.and("name", "like", "%" + name +"%");
		}

		return paperdefService.tableList(pageNum,pageSize,cnd,orderByColumn,isAsc,null);
	}
	
    /**
     * 详情试卷定义
     */
    @At("/detail/?")
    @Ok("th:/exam/paperdef/detail.html")
    public void detail(String id, HttpServletRequest req) {
		Paperdef paperdef = paperdefService.fetch(id);
        req.setAttribute("paperdef",paperdef);
    }
	/**
	 * 新增试卷定义
	 */
	@At("/add")
	@Ok("th:/exam/paperdef/add.html")
	public void add( HttpServletRequest req) {
		List<Qdb> list=qdbService.query(Cnd.where("pid","=",1));
		req.setAttribute("kms", list);
	}

	/**
	 * 新增保存试卷定义
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("exam:paperdef:add")
	@Slog(tag="试卷定义", after="新增保存试卷定义 id=${args[0].id}")
	public Object addDo(@Param("..") Paperdef paperdef,HttpServletRequest req) {
		try {
			//paperdef.setId(StringUtils.get17timeid());
			paperdef.setCreateBy(ShiroUtils.getSysUserId());
			paperdef.setCreateTime(new Date());
			paperdefService.insert(paperdef);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 修改试卷定义
	 */
	@At("/edit/?")
	@Ok("th://exam/paperdef/edit.html")
	public void edit(String id, HttpServletRequest req) {
		Paperdef paperdef = paperdefService.fetch(id);
		req.setAttribute("paperdef",paperdef);
		List<Qdb> list=qdbService.query(Cnd.where("pid","=",1));
		req.setAttribute("kms", list);
	}

	/**
	 * 修改保存试卷定义
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("exam:paperdef:edit")
	@Slog(tag="试卷定义", after="修改保存试卷定义")
	public Object editDo(@Param("..") Paperdef paperdef,HttpServletRequest req) {
		try {
			if(Lang.isNotEmpty(paperdef)){
				paperdef.setUpdateBy(ShiroUtils.getSysUserId());
				paperdef.setUpdateTime(new Date());
				paperdefService.update(paperdef);
			}
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 删除试卷定义
	 */
	@At("/remove")
	@Ok("json")
	@RequiresPermissions("exam:paperdef:remove")
	@Slog(tag ="试卷定义", after= "删除试卷定义:${array2str(args[0])}")
	public Object remove(@Param("ids")String[] ids, HttpServletRequest req) {
		try {
			paperdefService.delete(ids);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

}
